package com.byfan.barter.controller;

import com.byfan.barter.entity.Commodity;
import com.byfan.barter.entity.Orders;
import com.byfan.barter.service.CommodityService;
import com.byfan.barter.service.OrdersService;
import com.byfan.barter.utils.Message;
import com.byfan.barter.utils.MessageUtil;
import com.byfan.barter.utils.MyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: FBY
 * @Description
 * @Version 1.0
 * @Date: 2021/5/16 15:37
 */
@Api(description = "订单接口")
@RestController
@RequestMapping(value = "/orders")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @Autowired
    private CommodityService commodityService;

    @ApiOperation("新增订单")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public Message save(Orders orders){
        Orders o = ordersService.save(orders);
        return MessageUtil.success("新增订单成功",o);
    }

    @ApiOperation("根据用户openId获取所有订单，包括借入和借出的")
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Message findAll(String openId){
        List<Orders> ordersList = ordersService.getByOpenId(openId);
        List<Map> result = new ArrayList<>();
        if (ordersList!=null){
            for (Orders orders:ordersList){
                Map<String,Object> map = new HashMap<>();
                map.put("orders",orders);
                Commodity commodity = commodityService.getById(orders.getCommodityId());
                map.put("commodity",commodity);
                result.add(map);
            }
        }
        return MessageUtil.success(result);
    }

    @ApiOperation("修改订单信息")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Message update(Orders orders){
        Orders o = ordersService.getById(orders.getId());
        MyUtil.copyPropertiesIgnoreNull(orders,o);
        return MessageUtil.success("修改成功",ordersService.save(o));
    }

    @ApiOperation("根据id获取订单")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public Message getById(Integer id){
        Orders orders = ordersService.getById(id);
        if (orders!=null)
            return MessageUtil.success(orders);
        return MessageUtil.success("暂无数据");
    }

    @ApiOperation("根据用户openId获取借出的订单")
    @RequestMapping(value = "/getByFromOpenId",method = RequestMethod.GET)
    public Message getByFromOpenId(String openId){
        List<Orders> ordersList = ordersService.getByFromId(openId);
        List<Map> result = new ArrayList<>();
        if (ordersList!=null){
            for (Orders orders:ordersList){
                Map<String,Object> map = new HashMap<>();
                map.put("orders",orders);
                Commodity commodity = commodityService.getById(orders.getCommodityId());
                map.put("commodity",commodity);
                result.add(map);
            }
        }
        return MessageUtil.success(result);
    }

    @ApiOperation("根据用户openId获取借入的订单")
    @RequestMapping(value = "/getByToOpenId",method = RequestMethod.GET)
    public Message getByToOpenId(String openId){
        List<Orders> ordersList = ordersService.getByToId(openId);
        List<Map> result = new ArrayList<>();
        if (ordersList!=null){
            for (Orders orders:ordersList){
                Map<String,Object> map = new HashMap<>();
                map.put("orders",orders);
                Commodity commodity = commodityService.getById(orders.getCommodityId());
                map.put("commodity",commodity);
                result.add(map);
            }
        }
        return MessageUtil.success(result);
    }

    @ApiOperation("根据用户openId和订单状态获取订单")
    @RequestMapping(value = "/getByStatus",method = RequestMethod.GET)
    public Message getByStatus(String openId,String status){
        List<Orders> ordersList = ordersService.getByStatus(openId,status);
        List<Map> result = new ArrayList<>();
        if (ordersList!=null){
            for (Orders orders:ordersList){
                Map<String,Object> map = new HashMap<>();
                map.put("orders",orders);
                Commodity commodity = commodityService.getById(orders.getCommodityId());
                map.put("commodity",commodity);
                result.add(map);
            }
        }
        return MessageUtil.success(result);
    }

    @ApiOperation("根据用户openId和订单状态获取借出的订单")
    @RequestMapping(value = "/getByFromIdAndStatus",method = RequestMethod.GET)
    public Message getByFromIdAndStatus(String openId,String status){
        List<Orders> ordersList = ordersService.getByFromIdAndStatus(openId,status);
        List<Map> result = new ArrayList<>();
        if (ordersList!=null){
            for (Orders orders:ordersList){
                Map<String,Object> map = new HashMap<>();
                map.put("orders",orders);
                Commodity commodity = commodityService.getById(orders.getCommodityId());
                map.put("commodity",commodity);
                result.add(map);
            }
        }
        return MessageUtil.success(result);
    }

    @ApiOperation("根据用户openId和订单状态获取借入的订单")
    @RequestMapping(value = "/getByToIdAndStatus",method = RequestMethod.GET)
    public Message getByToIdAndStatus(String openId,String status){
        List<Orders> ordersList = ordersService.getByToIdAndStatus(openId,status);
        List<Map> result = new ArrayList<>();
        if (ordersList!=null){
            for (Orders orders:ordersList){
                Map<String,Object> map = new HashMap<>();
                map.put("orders",orders);
                Commodity commodity = commodityService.getById(orders.getCommodityId());
                map.put("commodity",commodity);
                result.add(map);
            }
        }
        return MessageUtil.success(result);
    }

    /**
     * 每天0点0分5秒自动执行，将所有剩余天数大于等于0的订单，剩余天数-1
     */
    @Scheduled(cron = "5 0 0 * * ?")
    public void countDown() {
        // 先获取所有剩余时间大于等于0的订单
        List<Orders> ordersList = ordersService.getAll();
        if (ordersList != null)
            for (Orders orders : ordersList) {
                // 将当前订单剩余时间天数-1
                orders.setRemainingTime(orders.getRemainingTime() - 1);
                // 保存修改过后的订单
                ordersService.save(orders);
            }
    }


}
